package com.app.micaihu.view.user.userinfo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.adapter.PagerAdapter.CommonFragmentAdapter;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.infor.AttentionStautsBean;
import com.app.micaihu.bean.infor.UserHomePage;
import com.app.micaihu.bean.infor.UserHomePageAttBean;
import com.app.micaihu.bean.infor.UserInfor;
import com.app.micaihu.bean.interactive.BaseOprationListener;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.configure.UmEventId;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.custom.view.HackyViewPager;
import com.app.micaihu.utils.ImageLoadUtils;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.micaihu.view.army.activity.ArmyDetailActivity;
import com.app.micaihu.view.army.activity.ArmyMedalListActivity;
import com.app.utils.biz.Biz;
import com.app.widget.LoadView;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.AppShareUtils;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.utils.HttpUtils;
import com.app.micaihu.utils.InteractiveUtils;
import com.app.micaihu.view.main.MainActivity;
import com.app.micaihu.view.newsdetail.ImageShowActivity;
import com.app.micaihu.view.user.userinfo.fragment.DynamicNewsFragment;
import com.app.micaihu.view.user.userinfo.fragment.DynamicPageFragment;
import com.app.micaihu.view.user.userinfo.fragment.DynamicPageTopicFragment;
import com.app.utils.scrollablelayout.ScrollableHelper;
import com.app.utils.scrollablelayout.ScrollableLayout;
import com.app.utils.stickheaderview.tab.SlidingTabLayout;
import com.app.utils.util.StringUtils;
import com.app.utils.util.ToastUtils;
import com.app.utils.util.ViewSizeUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.app.utils.util.osutils.StatusBarUtils;
import com.baidu.mobstat.StatService;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.google.gson.reflect.TypeToken;
import com.umeng.socialize.UMShareAPI;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的主页-动态
 * Created by linkai on 2016/8/24.
 */
public class MyHomepageDynamicActivity extends BaseOldActivity implements View.OnClickListener, AppShareUtils.DoMoreClickListener, DialogUtils.OnDialogBtnClickListener, ScrollableLayout.OnScrollListener {

    /*************
     * 页面控件
     ************/

    ImageView headUserImg;//头部用户头像
    CustomImageView junxianView;//军衔图标
    TextView headUserTitle;//头部用户昵称
    TextView attentionView;//关注类型

    /*************
     * 逻辑数据
     ************/
    String type = "2";//主页类型 1 为个人首页  2为其他人首页
    String toUid;
    UserHomePage userHomePage;//用户主页数据
    private View flJunxian;

    private View headBg;
    private TextView signature;
    private TextView attNum;
    private TextView fansNum;
    private SlidingTabLayout navTab;
    private ArrayList<Fragment> fragments;
    private TextView tvEditBtn;
    private TextView attTopView;
    private ImageView ivV;
    private ImageView ivSignature;
    private LinearLayout llSignature;
    private LoadView loadView;
    private ImageView userimg;
    private TextView username;
    private ImageView ivMore;
    private AppShareUtils mShareUtils;
    private LinearLayout title;
    private LinearLayout llHeaderTitle;
    private LinearLayout llMedal;
    private boolean seeTopic;
    private ImageView ivArrow;
    private HackyViewPager mViewPager;
    private CommonFragmentAdapter mAdapter;
    private ScrollableLayout scrollableLayout;
    private ImageView ivLeft;

    private TextView tvMilitary, tvWarZone, tvArmyGroupInfo, tvUserId;
    private String gid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isInit = false;
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarLightMode(this, true);
        Intent intent = getIntent();
        if (intent != null) {
            toUid = intent.getStringExtra(Config.Extra.PARAMETER1);
            seeTopic = intent.getBooleanExtra(Config.Extra.PARAMETER2, false);
        }
        setContentView(R.layout.activity_myhomepage_dynamic);
        initView();
        loadData();
    }


    @Override
    protected void onResume() {
        super.onResume();
        //刷新本地数据
        flushUserInfo();

    }

    /**
     * 初始化页面
     */
    private void initView() {
        tvUserId = findViewById(R.id.tvUserId);
        int length = toUid.length();
        if (length >= 5) {
            tvUserId.setText("军牌 " + toUid.substring(length - 5, length));
        }

        scrollableLayout = (ScrollableLayout) findViewById(R.id.scrollableLayout);
        scrollableLayout.setOnScrollListener(this);
        //分版本的 设置滑动最大距离
        scrollableLayout.setScrollTitleHeight(ViewSizeUtils.dp2px(this, 44) + StatusBarUtils.getStatusBarHeight(this));
        headBg = findViewById(R.id.title);
        headBg.setPadding(0, BarUtils.getStatusBarHeight(), 0, 0);
        tvMilitary = findViewById(R.id.tvMilitary);
        tvWarZone = findViewById(R.id.tvWarZone);
        tvArmyGroupInfo = findViewById(R.id.tvArmyGroupInfo);

        title = findViewById(R.id.rl_homepage_head);
        title.setOnClickListener(this);
        headUserImg = findViewById(R.id.civ_headimg);
        flJunxian = findViewById(R.id.ll_junxian);
        flJunxian.setOnClickListener(this);
        junxianView = (CustomImageView) findViewById(R.id.civ_junxian);
        headUserTitle = (TextView) findViewById(R.id.tv_headtitle);
        attentionView = (TextView) findViewById(R.id.home_header_attention_bt);
        attTopView = (TextView) findViewById(R.id.home_header_attention_bt_top);
        attentionView.setOnClickListener(this);
        attTopView.setOnClickListener(this);
        //203新增
        //签名
        signature = (TextView) findViewById(R.id.signature);

        attNum = (TextView) findViewById(R.id.tv_attentionnum);
        fansNum = (TextView) findViewById(R.id.tv_fansnum);

        ivArrow = (ImageView) findViewById(R.id.iv_arraw);

        navTab = (SlidingTabLayout) findViewById(R.id.navig_tab);
        mViewPager = (HackyViewPager) findViewById(R.id.view_pager);

        tvEditBtn = (TextView) findViewById(R.id.edit_btn);
        tvEditBtn.setOnClickListener(this);

        ivV = (ImageView) findViewById(R.id.iv_v);
        ivSignature = (ImageView) findViewById(R.id.iv_signature);

        llSignature = (LinearLayout) findViewById(R.id.ll_signature);
        llSignature.setOnClickListener(this);

        loadView = (LoadView) findViewById(R.id.ld_loadview);
        loadView.setErrorPageClickListener(this);
        ivMore = (ImageView) findViewById(R.id.iv_more);
        ivMore.setOnClickListener(this);

        llHeaderTitle = (LinearLayout) findViewById(R.id.ll_title_info);
        ivLeft = (ImageView) findViewById(R.id.iv_appleft);
        llMedal = findViewById(R.id.llMedal);
        llMedal.setOnClickListener(this);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (scrollableLayout != null && fragments != null) {
                    scrollableLayout.getHelper().setCurrentScrollableContainer((ScrollableHelper.ScrollableContainer) fragments.get(position));

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        findViewById(R.id.llAttention).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentFollow = new Intent(MyHomepageDynamicActivity.this, AttentionAndFansSwitchPageActivity.class);
                intentFollow.putExtra(Config.Extra.PARAMETER1, "0");
                intentFollow.putExtra(Config.Extra.PARAMETER2, toUid);
                startActivity(intentFollow);
            }
        });
        findViewById(R.id.llFans).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentFans = new Intent(MyHomepageDynamicActivity.this, AttentionAndFansSwitchPageActivity.class);
                intentFans.putExtra(Config.Extra.PARAMETER1, "1");
                intentFans.putExtra(Config.Extra.PARAMETER2, toUid);
                startActivity(intentFans);
            }
        });
        tvWarZone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!com.blankj.utilcode.util.StringUtils.isEmpty(gid) && !com.blankj.utilcode.util.StringUtils.isEmpty(toUid)) {
                    Intent intent = new Intent(MyHomepageDynamicActivity.this, ArmyDetailActivity.class);
                    intent.putExtra(Config.Extra.toUid, toUid);
                    intent.putExtra(Config.Extra.id, gid);
                    ActivityUtils.startActivity(intent);
                }
            }
        });
        tvArmyGroupInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!com.blankj.utilcode.util.StringUtils.isEmpty(gid) && !com.blankj.utilcode.util.StringUtils.isEmpty(toUid)) {
                    Intent intent = new Intent(MyHomepageDynamicActivity.this, ArmyDetailActivity.class);
                    intent.putExtra(Config.Extra.toUid, toUid);
                    intent.putExtra(Config.Extra.id, gid);
                    ActivityUtils.startActivity(intent);
                }

            }
        });
    }

    /**
     * 设置title 的透明度
     *
     * @param alpha
     */
    private void transparentTitle(float alpha) {
        headBg.getBackground().setAlpha((int) (alpha * 255));
        headUserTitle.setAlpha(alpha);
        headUserImg.setAlpha(alpha);
        attTopView.setAlpha(alpha);
        if (alpha > 0.2f) {
//            BarUtils.setStatusBarLightMode(this, true);
            ivMore.setImageResource(R.drawable.detail_menu_black_selector);
            ivLeft.setImageResource(R.drawable.common_back_selector);
        } else {
//            BarUtils.setStatusBarLightMode(this, false);
            ivMore.setImageResource(R.drawable.detail_menu_white_selector);
            ivLeft.setImageResource(R.drawable.left_back_click_white_selector);
        }
    }

    /**
     * 加载数据
     */
    private void loadData() {
        HashMap<String, String> parms = new HashMap<>();
        if (UserInforManager.getInstance().isLogin()) {
            parms.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
            if (TextUtils.equals(toUid, UserInforManager.getInstance().getUserInfor().getUid())) {
                type = "1";
                //如果是查看自己的主页则隐藏 关注按钮
                attentionView.setVisibility(View.GONE);
                tvEditBtn.setVisibility(View.VISIBLE);
            } else {
                type = "2";
                tvEditBtn.setVisibility(View.GONE);
                attentionView.setVisibility(View.VISIBLE);

                attentionView.getCompoundDrawables()[0].setLevel(4);
                attentionView.setText("关注");
            }
        }
        parms.put("toUid", toUid);
        parms.put("type", type);
        HttpListener<DataBean<UserHomePage>> httpListener = new HttpListener<DataBean<UserHomePage>>() {

            @Override
            public void onStart() {
                if (loadView != null) {
                    loadView.showLoadPage(null);
                    goneTitle();
                }
            }

            @Override
            public void onError(VolleyError error) {
                if (loadView != null) {
                    loadView.showErrorPage(getString(R.string.neterror));
                    goneTitle();
                }
            }

            @Override
            public void onSuccess(DataBean<UserHomePage> response) {
                if (response.noError() && response.getData() != null) {
                    userHomePage = response.getData();
                    setData(response);
                } else {
                    ToastUtils.showText(response.getNnderstoodMsg());
                    if (loadView != null) {
                        loadView.showErrorPage(response.getNnderstoodMsg());
                    } else {
                        ToastUtils.showText(response.getNnderstoodMsg());
                    }
                }
            }
        };

        HttpUtils.addPostCacheRequest(URIContant.MY_HOMEN_PAGE, new TypeToken<DataBean<UserHomePage>>() {
        }.getType(), mPageName, parms, httpListener);
    }

    private void goneTitle() {
        if (llHeaderTitle != null && attTopView != null && ivMore != null && headBg != null && ivLeft != null) {
            llHeaderTitle.setVisibility(View.GONE);
//            ivMore.setVisibility(View.GONE);
            headBg.setBackgroundColor(getResources().getColor(R.color.common_bg_color_2));
            ivLeft.setImageResource(R.drawable.common_back_selector);
        }
    }


    /**
     * 显示正常的title
     */
    private void showTitle() {
        if (llHeaderTitle != null && headBg != null && ivLeft != null && ivMore != null) {
            llHeaderTitle.setVisibility(View.VISIBLE);
            headBg.setBackgroundColor(getResources().getColor(R.color.white));
            ivMore.setVisibility(View.VISIBLE);
            ivLeft.setImageResource(R.drawable.left_back_click_white_selector);
            transparentTitle(0F);
        }
    }


    /**
     * 从其他页面返回后 刷新数据
     */
    private void flushUserInfo() {
        if (!isOther()) {//当前页面是自己的页面
            UserInfor userInfor = UserInforManager.getInstance().getUserInfor();
            if (userInfor != null) {
                if (userimg != null)
                    ImageLoadUtils.loadCircleImage(userInfor.getHeadPic(), userimg, R.drawable.task_head_nologging_nor);
                if (headUserImg != null)
                    ImageLoadUtils.loadCircleImage(userInfor.getHeadPic(), headUserImg, R.drawable.task_head_nologging_nor);

                if (headUserTitle != null) headUserTitle.setText(userInfor.getNickName());
                if (username != null) username.setText(userInfor.getNickName());

                if (signature != null && ivSignature != null)
                    if (TextUtils.isEmpty(userInfor.getSignature())) {
                        signature.setText(AppApplication.getInstance().getString(R.string.user_signature_edit));
                        ivSignature.setVisibility(View.VISIBLE);
                        ivSignature.setAlpha(1.0f);
                        signature.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.common_bg_green_5));
                    } else {
                        signature.setText(userInfor.getSignature());
                        ivSignature.setVisibility(View.GONE);
                        signature.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.common_font_color_3));
                    }
            }
        }

    }


    /**
     * 设置数据
     *
     * @param dataBean
     */
    private void setData(DataBean<UserHomePage> dataBean) {
        if (userHomePage == null || attentionView == null) {
            if (loadView != null) {
                loadView.showErrorPage(dataBean.getNnderstoodMsg());
            } else {
                ToastUtils.showText(dataBean.getNnderstoodMsg());
            }
            ToastUtils.showText(dataBean.getNnderstoodMsg());
            return;
        }
        gid = userHomePage.getGid();
//        userHomePage.setIsForbid("1");
        if (userHomePage.getIsForbid()) {
            loadView.showEmptyPage(TypeConstant.EMPTYVIEW_BANSPEECH, getString(R.string.my_home_fengjin), null, getString(R.string.msg_empty_go));
            loadView.setEmptyGoClickListener(this);
            return;
        }

        //设置toUid-为拉黑
        userHomePage.setIToUid(toUid);

        userimg = findViewById(R.id.img_head);
//        LoadImageUtils.getInstance().loadCircleImageWithBorder(userimg, userHomePage.getHeadPic(), AppApplication.getInstance().getResources().getColor(R.color.common_white_color), 5);
        ImageLoadUtils.loadCircleImage(userHomePage.getHeadPic(), userimg, R.drawable.task_head_nologging_nor);

//        LoadImageUtils.getInstance().loadCircleImage(userimg, userHomePage.getHeadPic());
        LoadImageUtils.getInstance().loadImage(junxianView, userHomePage.getRankIcon());
        userimg.setOnClickListener(this);
//        LoadImageUtils.getInstance().loadCircleImage(headUserImg, userHomePage.getHeadPic());

        ImageLoadUtils.loadCircleImage(userHomePage.getHeadPic(), headUserImg, R.drawable.task_head_nologging_nor);
        username = (TextView) findViewById(R.id.txt_nickname);
        username.setText(userHomePage.getNickName());
        headUserTitle.setText(userHomePage.getNickName());
        TextView militaryTime = (TextView) findViewById(R.id.txt_time);
        militaryTime.setText(userHomePage.getMilitaryTime());
        TextView rankname = (TextView) findViewById(R.id.tv_rankname);
        rankname.setText(userHomePage.getRankName());


        if (isOther()) {
            signature.setText(TextUtils.isEmpty(userHomePage.getSignature()) ? AppApplication.getInstance().getString(R.string.user_signature) : userHomePage.getSignature());
            ivSignature.setVisibility(View.GONE);
            signature.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.common_font_color_3));
            ivArrow.setVisibility(View.GONE);
        } else {//自己个人主页
            ivArrow.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(userHomePage.getSignature())) {
                signature.setText(AppApplication.getInstance().getString(R.string.user_signature_edit));
                ivSignature.setVisibility(View.VISIBLE);
                signature.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.common_bg_green_5));
            } else {
                signature.setText(userHomePage.getSignature());
                ivSignature.setVisibility(View.GONE);
                signature.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.common_font_color_3));
            }
        }

        fansNum.setText(TextUtils.isEmpty(userHomePage.getFans()) ? "0" : userHomePage.getFans());
        attNum.setText(TextUtils.isEmpty(userHomePage.getFollows()) ? "0" : userHomePage.getFollows());

        if ("1".equals(type)) {
            tvEditBtn.setVisibility(View.VISIBLE);
            attentionView.setVisibility(View.GONE);

        } else if ("2".equals(type)) {
            tvEditBtn.setVisibility(View.GONE);
            attentionView.setVisibility(View.VISIBLE);
            setAttentionView();
        }

        //判断作者是不是Vip
        if (userHomePage.getVip()) {
            ivV.setVisibility(View.VISIBLE);
        } else {
            ivV.setVisibility(View.GONE);

        }

        //判断是否是作者还是个人
        if (userHomePage.getNavList() != null && !userHomePage.getNavList().isEmpty()) {
            int position = 0;
//            if(isAuthor){//是作者
            if (fragments == null) {
                fragments = new ArrayList<>();
            }
            fragments.clear();
            for (int i = 0; i < userHomePage.getNavList().size(); i++) {
                if (userHomePage.getNavList().get(i).getSelected()) {
                    position = i;
                }

                String navType = userHomePage.getNavList().get(i).getNavType();
                if (TextUtils.equals("0", navType)) {//证明这个是一条动态
                    DynamicPageFragment dynamicPageFragment = DynamicPageFragment.newInstance(userHomePage.getNavList().get(i), toUid);
                    if (i == 0) {
                        dynamicPageFragment.initData(userHomePage.getCmtList());
                    }
                    fragments.add(dynamicPageFragment);
                } else if (TextUtils.equals("1", navType)
                        || TextUtils.equals("2", navType)
                        || TextUtils.equals("3", navType)
                        || TextUtils.equals("4", navType)) {
                    DynamicNewsFragment dynamicNewsFragment = DynamicNewsFragment.newInstance(userHomePage.getNavList().get(i), toUid);
                    if (i == 0) {
                        dynamicNewsFragment.initData(userHomePage.getNewsList());
                    }
                    fragments.add(dynamicNewsFragment);
                } else if (TextUtils.equals("5", navType)) {//这是一个话题
                    DynamicPageTopicFragment dynamicPageFragment = DynamicPageTopicFragment.newInstance(userHomePage.getNavList().get(i), toUid);
                    fragments.add(dynamicPageFragment);
                    if (seeTopic) {
                        position = i;
                    }
                }
            }

            if (mAdapter == null) {
                //创建 title的 数组,进行每个item的title
                String[] titles = new String[userHomePage.getNavList().size()];
                for (int i = 0; i < userHomePage.getNavList().size(); i++) {
                    if (userHomePage.getNavList().get(i) != null) {
                        titles[i] = userHomePage.getNavList().get(i).getName();
                    }
                }
                mAdapter = new CommonFragmentAdapter(getSupportFragmentManager(), fragments, titles);
                mViewPager.setAdapter(mAdapter);
                scrollableLayout.getHelper().setCurrentScrollableContainer((ScrollableHelper.ScrollableContainer) fragments.get(position));
                navTab.setViewPager(mViewPager);
                mViewPager.setCurrentItem(position);
                mViewPager.setOffscreenPageLimit(userHomePage.getNavList().size());
                navTab.setTabSelected(position);
            }
        }
        showTitle();

        List<String> honourIconList = userHomePage.getHonourIconList();
        if (honourIconList != null) {
            llMedal.removeAllViews();
            for (int i = 0; i < honourIconList.size(); i++) {
                ImageView imageView = new ImageView(this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(SizeUtils.dp2px(35f), SizeUtils.dp2px(38f));
                layoutParams.rightMargin = SizeUtils.dp2px(10f);
                layoutParams.gravity = Gravity.CENTER_VERTICAL;
                imageView.setLayoutParams(layoutParams);
                ImageLoadUtils.loadImage(honourIconList.get(i), imageView);
                llMedal.addView(imageView);
            }
        }

        if (StringUtils.isEmpty(userHomePage.getMilitaryServices())) {
            tvMilitary.setVisibility(View.GONE);
        } else {
            tvMilitary.setVisibility(View.VISIBLE);
            tvMilitary.setText(userHomePage.getMilitaryServices());
        }
        if (StringUtils.isEmpty(userHomePage.getWarZone())) {
            tvWarZone.setVisibility(View.GONE);
        } else {
            tvWarZone.setVisibility(View.VISIBLE);
            tvWarZone.setText(userHomePage.getWarZone());
        }

        if (StringUtils.isEmpty(userHomePage.getArmyGroup())) {
            tvArmyGroupInfo.setVisibility(View.GONE);
        } else {
            tvArmyGroupInfo.setVisibility(View.VISIBLE);
            tvArmyGroupInfo.setText(userHomePage.getArmyGroup());
        }

        loadView.showSuccess();
    }

    private boolean isOther() {
        return !(UserInforManager.getInstance().isLogin() && TextUtils.equals(UserInforManager.getInstance().getUserInfor().getUid(), toUid));
    }

    private void setAttentionView() {
        if (userHomePage != null && attentionView != null && attTopView != null) {

            if ("0".equals(userHomePage.getIsAttention())) {
                //未关注
                attentionView.getCompoundDrawables()[0].setLevel(4);
                attentionView.setText("关注");
                attentionView.setSelected(true);
                attTopView.getCompoundDrawables()[0].setLevel(3);
                attTopView.setText("关注");
            } else if ("1".equals(userHomePage.getIsAttention())) {
                //已关注
                attentionView.getCompoundDrawables()[0].setLevel(5);
                attentionView.setText("已关注");
                attentionView.setSelected(false);
                attTopView.getCompoundDrawables()[0].setLevel(6);
                attTopView.setText("已关注");
            } else if ("2".equals(userHomePage.getIsAttention())) {

                //互相关注
                attentionView.getCompoundDrawables()[0].setLevel(1);
                attentionView.setText("互相关注");
                attentionView.setSelected(false);
                attTopView.getCompoundDrawables()[0].setLevel(2);
                attTopView.setText("互相关注");
            }
        }

    }


    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.rl_homepage_head:
                break;
            case R.id.iv_more://弹出分享
                share();
                break;
            case R.id.empty_text_go://随便看看
                startActivity(new Intent(this, MainActivity.class));
                break;
            case R.id.ll_signature:
                if (!isOther()) {
                    startActivity(new Intent(this, EditorUserInfoActivity.class));
                }
                break;
            case R.id.edit_btn:
                if (TextUtils.equals(type, "1") && UserInforManager.getInstance().isLogin()) {
                    startActivityForResult(new Intent(this, UserInforActivity.class), Config.ResultCode.REQUESTCODE_1);
                }
                break;
            case R.id.error_page:
                if (UserInforManager.getInstance().isLogin()) {
                    loadData();
                } else {
                    UserInforManager.getInstance().toLoginPage(this);
                }
                break;
            case R.id.img_head:
                Intent intent = new Intent(this, ImageShowActivity.class);
                ArrayList<String> arrayList = new ArrayList<>();
                arrayList.add(userHomePage.getHeadPic());
                intent.putStringArrayListExtra(Config.Extra.PARAMETER1, arrayList);
                intent.putExtra(Config.Extra.PARAMETER2, 0);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                overridePendingTransition(R.anim.base_alpha_in, R.anim.base_slide_remain);// 前一个参数是下一个Activity的效果
                StatService.onEvent(this, "051", "点击头像", 1);//统计事件的点击次数
                break;
            case R.id.home_header_attention_bt://关注
            case R.id.home_header_attention_bt_top://关注
                MobclickAgentUtils.event(UmEventId.attentionbutton_click, "个人中心");
                attentionOrCancle();
                break;
            case R.id.llMedal://勋章
                Intent intent1 = new Intent(this, ArmyMedalListActivity.class);
                intent1.putExtra(Config.Extra.toUid, toUid);
                intent1.putExtra(Config.Extra.id, "1005");
                intent1.putExtra(Config.Extra.showModal, true);
                startActivity(intent1);
                break;
        }
    }

    private void share() {
        if (userHomePage == null) return;
        if (mShareUtils == null) {
            mShareUtils = new AppShareUtils(this);
        }
        this.userHomePage.setArticleId(toUid);
        if (isOther()) {
            if (userHomePage.getIsAuthor()) {
                mShareUtils.showShareDialog(this.userHomePage, TypeConstant.PAGE_AUTHOR_PAGE);
            } else {
                mShareUtils.showShareDialog(this.userHomePage, TypeConstant.PAGE_OTHER_HOME);
                mShareUtils.setOnDoMoreClickListener(this);
            }
        } else {
            mShareUtils.showShareDialog(this.userHomePage, TypeConstant.PAGE_HOME);
        }


    }


    /**
     * 刷新关注状态
     */
    private void flushData() {
        HashMap<String, String> parms = new HashMap<>();
        if (UserInforManager.getInstance().isLogin()) {
            parms.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
            if (TextUtils.equals(toUid, UserInforManager.getInstance().getUserInfor().getUid())) {
                type = "1";
            } else {
                type = "2";
            }
        }
        parms.put("toUid", toUid);
        parms.put("type", type);
        HttpListener<DataBean<UserHomePage>> httpListener = new HttpListener<DataBean<UserHomePage>>() {

            @Override
            public void onStart() {
            }

            @Override
            public void onError(VolleyError error) {
            }

            @Override
            public void onSuccess(DataBean<UserHomePage> response) {
                if (attentionView == null || attentionView == null) {
                    return;
                }


                if (response.noError() && response.getData() != null) {
                    userHomePage = response.getData();


                    if ("1".equals(type)) {
                        tvEditBtn.setVisibility(View.VISIBLE);
                        attentionView.setVisibility(View.GONE);
                        ivMore.setVisibility(View.VISIBLE);

                    } else if ("2".equals(type)) {
                        tvEditBtn.setVisibility(View.GONE);
                        attentionView.setVisibility(View.VISIBLE);
                        setAttentionView();
                    }
                }
            }
        };

        HttpUtils.addPostRequest(URIContant.MY_HOMEN_PAGE, new TypeToken<DataBean<UserHomePage>>() {
        }.getType(), "", parms, httpListener);
    }

    /**
     * 关注/取消关注
     */
    private void attentionOrCancle() {
        if (userHomePage == null) {
            return;
        }
        if (!UserInforManager.getInstance().isLogin() || UserInforManager.getInstance().getUserInfor() == null) {
            UserInforManager.getInstance().toLoginPage(this);
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
        params.put("toUid", toUid);
        String currentRequestType;
        if (TextUtils.equals(userHomePage.getIsAttention(), "0")) {     //未关注->点击去关注
            currentRequestType = "1";
            StatService.onEvent(this, "051", "关注", 1);//统计事件的点击次数
        } else {
            //取消关注
            currentRequestType = "2";
            StatService.onEvent(this, "051", "取消关注", 1);//统计事件的点击次数
        }
        params.put("type", currentRequestType);

        HttpListener<DataBean<AttentionStautsBean>> attentionListener = new HttpListener<DataBean<AttentionStautsBean>>() {
            @Override
            public void onStart() {
                super.onStart();

                DialogUtils.getInstance().showProgressDialog(MyHomepageDynamicActivity.this, getResources().getString(R.string.homepage_attention_start));

            }

            @Override
            public void onError(VolleyError error) {
                DialogUtils.getInstance().dismissProgressDialog();
                ToastUtils.showText(getResources().getString(R.string.neterror));
            }

            @Override
            public void onSuccess(DataBean<AttentionStautsBean> response) {
                if (userHomePage == null || attentionView == null) {
                    return;
                }
                DialogUtils.getInstance().dismissProgressDialog();
                if (response.noError() && response.getData() != null) {
                    AttentionStautsBean attentionStautsBean = response.getData();
                    userHomePage.setIsAttention(attentionStautsBean.getStatus());
                    int fensi = StringUtils.toInt(userHomePage.getFans(), 0);
                    if ("0".equals(attentionStautsBean.getStatus())) {
                        //未关注
                        attentionView.getCompoundDrawables()[0].setLevel(4);
                        attentionView.setText("关注");
                        attentionView.setSelected(true);
                        attTopView.getCompoundDrawables()[0].setLevel(3);
                        attTopView.setText("关注");
                        fensi = StringUtils.toInt(userHomePage.getFans(), 0) - 1;

                        ivMore.setVisibility(View.VISIBLE);

                        ToastUtils.makeText("已取消关注");
                    } else if ("1".equals(attentionStautsBean.getStatus())) {
                        //已关注
                        attentionView.getCompoundDrawables()[0].setLevel(5);
                        attentionView.setText("已关注");
                        attentionView.setSelected(false);
                        attTopView.getCompoundDrawables()[0].setLevel(6);
                        attTopView.setText("已关注");
                        fensi = StringUtils.toInt(userHomePage.getFans(), 0) + 1;
                        ivMore.setVisibility(View.VISIBLE);

                        attTopView.setVisibility(View.GONE);
                        ToastUtils.makeText("关注成功");
                    } else if ("2".equals(attentionStautsBean.getStatus())) {
                        //互相关注
                        attentionView.getCompoundDrawables()[0].setLevel(1);
                        attentionView.setText("互相关注");
                        attentionView.setSelected(false);
                        attTopView.getCompoundDrawables()[0].setLevel(2);
                        attTopView.setText("互相关注");
                        fensi = StringUtils.toInt(userHomePage.getFans(), 0) + 1;

                        ivMore.setVisibility(View.VISIBLE);
                        attTopView.setVisibility(View.GONE);
                        ToastUtils.makeText("关注成功");
                    }
                    userHomePage.setFans(fensi + "");
                } else {
                    //                    ToastUtils.makeText(getResources().getString(R.string.dataerror));
                    ToastUtils.makeText(response.getNnderstoodMsg());
                }
            }
        };

        HttpUtils.addPostRequest(URIContant.ATTENTION_OR_NO, new TypeToken<DataBean<AttentionStautsBean>>() {
        }.getType(), mPageName, params, attentionListener);


    }


    @Override
    public void onDeleteClick() {
        //不做什么
    }

    @Override
    public void onDefriendClick() {
        if (UserInforManager.getInstance().isLogin()
                && userHomePage != null) {
            showDefriendDialog();

        } else {
            UserInforManager.getInstance().toLoginPage(MyHomepageDynamicActivity.this);
        }
    }


    /**
     * 拉黑的操作
     */
    private BaseOprationListener defriendListener = new BaseOprationListener() {
        @Override
        public void onOprationEnd() {
            if (userHomePage == null) return;
            if (userHomePage.getIsBlack()) {
                userHomePage.setIsBlack("0");
            } else {
                userHomePage.setIsBlack("1");
            }
        }
    };


    /**
     * 展示拉黑的dialog
     */
    private void showDefriendDialog() {
        HashMap<String, String> style = new HashMap<>();
        style.put("title", getResources().getString(R.string.dialog_defriend_title));
        style.put("desc", getString(R.string.dialog_defriend_msg));
        style.put("btnPosi", getString(R.string.common_confirm));
        style.put("btnNega", getString(R.string.common_cancel));
        DialogUtils.getInstance().setOnDialogBtnClick(this);
        DialogUtils.getInstance().showCustomDialog(this, style);
    }


    @Override
    public void onPositiveClick() {

        InteractiveUtils.getInstance().defriend(MyHomepageDynamicActivity.this, userHomePage, null);
    }

    @Override
    public void onNegativeClick() {

    }


    /**
     * 跳转个人主页
     *
     * @param activity
     * @param toUid
     */
    public static void goToUserHomePage(Activity activity, String toUid) {
        Intent intent = new Intent();
        intent.setClass(activity, MyHomepageDynamicActivity.class);
        intent.putExtra(Config.Extra.PARAMETER1, toUid);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
    }

    /**
     * 跳转个人主页
     *
     * @param activity
     * @param toUid
     */
    public static void goToUserHomePage(Activity activity, String toUid, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(activity, MyHomepageDynamicActivity.class);
        intent.putExtra(Config.Extra.PARAMETER1, toUid);
        activity.startActivityForResult(intent, requestCode);
        activity.overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
    }

    @Override
    public void onScroll(int currentY, int maxY) {
//        LogUtils.e("gzq", "current Y = " + currentY + "    maxY = " + maxY);
        if (headBg != null && headUserImg != null && headUserTitle != null && attTopView != null && ivMore != null) {
            float currentYF = (float) currentY;
            transparentTitle(currentYF / maxY);

            LogUtils.e("currentYF->" + currentYF + " maxY->" + maxY + " al->" + (currentYF / maxY));
            if ("1".equals(type)) {
                ivMore.setVisibility(View.VISIBLE);
            } else if ("2".equals(type)) {
                if (userHomePage != null) {
                    if ("0".equals(userHomePage.getIsAttention())) {
                        //未关注
                        if (headUserTitle.getAlpha() > 0.4f) {
                            attTopView.setVisibility(View.VISIBLE);
//                            ivMore.setVisibility(View.GONE);
                        } else {
                            attTopView.setVisibility(View.GONE);
                            ivMore.setVisibility(View.VISIBLE);
                        }

                    } else if ("1".equals(userHomePage.getIsAttention())) {
                        //已关注
                        ivMore.setVisibility(View.VISIBLE);
                    } else if ("2".equals(userHomePage.getIsAttention())) {
                        //互相关注
                        ivMore.setVisibility(View.VISIBLE);
                    }
                }
            }
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.ResultCode.REQUESTCODE_1) {
            //如果是请求关注页面回来
            if ("1".equals(type)) {
                flushData();
            }
        } else if (resultCode == RESULT_OK) {
            //请求登录页面回来
//            flushData();
            if (userHomePage == null) {//证明第一次进入个人主页
                loadData();
            } else {
                flushData();
            }
        }
        //设置友盟回调
        try {
            if (data != null) {
                UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onBackPressed() {
        if (userHomePage != null) {
            Intent intent = new Intent();
            intent.putExtra(Config.Extra.PARAMETER1, userHomePage.getIsAttention());
            setResult(RESULT_OK, intent);
        }
        if (ActivityUtils.isActivityExistsInStack(MainActivity.class)) {
            finish();
            overridePendingTransition(0, R.anim.base_slide_right_out);
        } else {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        }


    }

}
